//该文件用于创建Vuex中最为核心的store
import Vuex from "vuex"//引入vuex
import Vue from "vue";

Vue.use(Vuex)//使用vuex 插件

const actions = {//准备actions对象—用于响应组件中用户的动作
    jia(context, value) {
        //console.log("@",context,value)
        context.commit("JIA", value)
    },
    jian(context, value) {
        //console.log("@",context,value)
        context.commit("JIAN", value)
    },
    jiaOdd(context, value) {
        console.log("jiaOdd")
        context.dispatch("demo1", value)
        if (context.state.sum % 2) {
            context.commit("JIA", value)
        }
    },
    jiaWait(context, value) {
        setTimeout(() => {
            context.commit("JIA", value)
        }, 1000)
    },
    demo1(context, value) {
        console.log("demo1")
    }
}

const mutations = {
    //准备mutations对象—用于操作数据（state）
    JIA(state, value) {
        //console.log("@",state,value)
        state.sum += value
    },
    JIAN(state, value) {
        state.sum -= value
    },
    ADDPersonList(state, value){
        state.PersonList.push(value)
    }
}
const state = {
    sum: 0,//当前的和
    school:"宿舍",
    subject:'vue',
    PersonList:[{id:"001",name:"楠楠"}]
}//准备state对象—用于存储具体的数据

//准备getters—用于将state中的数据进行加工
const getters = {
    bigSum(state) {
        return state.sum * 10
    }
}

//创建并暴露state
export default new Vuex.Store({
    actions: actions,
    mutations: mutations,
    state: state,
    getters:getters,
})

